// pages/reservationResult/reservationResult.js
let app = getApp()
import { requestApi } from '../../api.js'
import { formatImage } from '../../utils/formatImage'
import { formatPrice } from '../../utils/util'

Page({

  /**
   * 页面的初始数据
   */
  data: {
    reservationStatus: '',
    reservationData: [],
    tradeNo: '',
    activeStatus: 1,
    loading: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let channel = options.channel || 'wx_xcx'
    let reservationStatus = options.reservationStatus
    let activity_id = options.activity_id
    let tradeNo = options.trade_no 

    this.setData({
      reservationStatus: reservationStatus,
      tradeNo: tradeNo
    })

    this.getReservationWin(reservationStatus, activity_id)
  },

  getReservationWin: function (reservationStatus, activity_id) {
    let url, reservationData
    
    switch(reservationStatus) {
      case '1':
      case '4':
      case '5':
        url = requestApi.reservationWinUrl

        app.request({
          url: url,
          data: {
            reservation_id: '' + activity_id
          }
        }, (data) => {
          reservationData = data.original.data.item_list

          if (reservationData && reservationData.length) {
            reservationData.forEach((item, index) => {
              if (index == 0) {
                if (item.data.image_list && item.data.image_list.length && item.data.image_list[0] !== null) {
                  item.data.image_list[0].new_path = formatImage.cdnImage(item.data.image_list[0].path, formatImage.THUMBNAIL_L)
                }
              }

              if (index == 1) {
                if (item.data.res_cover_image && item.data.res_cover_image.path) {
                  item.data.res_cover_image.new_path = formatImage.cdnImage(item.data.res_cover_image.path, formatImage.THUMBNAIL_L)
                }
              }

            })
          }
          
          this.setData({
            reservationData: reservationData,
            loading: true
          })
        })
      break;
      case '3':
        url = requestApi.reservationNotWinUrl

        app.request({
          url: url,
          data: {
            reservation_id: activity_id
          }
        }, (data) => {
          reservationData = data.original.data.item_list

          if (reservationData && reservationData.length) {
            reservationData.forEach((item) => {
              if (item.type == 34) {
                if (item.data.product_cover_image && item.data.product_cover_image.path) {
                  item.data.product_cover_image.new_path = formatImage.cdnImage(item.data.product_cover_image.path, formatImage.THUMBNAIL_L)
                }
                item.data.goods_price = item.data.goods_price ? formatPrice(item.data.goods_price / 100) : ''
              }
            })
          }

          this.setData({
            reservationData: reservationData,
            loading: true
          })
        })
      break;
    }

    
  },

  paymentTap: function () {
    let url = '../trade/tradeDetail/tradeDetail?trade_no=' + this.data.tradeNo + '&channel=' + this.data.channel + '&activeStatus=' + this.data.activeStatus

    wx.navigateTo({
      url: url,
    })
  },

  goodsTap: function (e) {
    let gid = e.currentTarget.dataset.id

    wx.navigateTo({
      url: '../goods/goodsDetail/goodsDetail?gid=' + gid,
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  // onShareAppMessage: function () {
  
  // }
})